package com.ece.ecofitness.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ece.ecofitness.dao.ArticleDaoImp;
import com.ece.ecofitness.model.Article;

@Service
public class ArticleServiceImp implements ArticleService{

	@Autowired
	private ArticleDaoImp articleDao;

	public boolean addArticle(String admin, String label, String description,int point,FileItem photo,String dir) {
		if(photo!=null){
		String path=dir+"images\\";
		File folder = new File(path);
		if(!folder.exists())
			folder.mkdirs();
		
		String pathArticle=path+"\\"+FilenameUtils.getName(photo.getName());
		
		File picture = new File(pathArticle);
		
		if(picture.exists()){
			pathArticle=FilenameUtils.removeExtension(pathArticle);
			pathArticle+=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
			pathArticle+="."+photo.getName().split("\\.")[photo.getName().split("\\.").length-1];
		}
		
		
		try {
			picture=new File(pathArticle);
			picture.createNewFile();
			OutputStream out = new FileOutputStream(pathArticle);
			InputStream in=photo.getInputStream();
			ImageIO.write(ImageIO.read(in),"jpg",out );
			out.close();in.close();
			return articleDao.addArticle(admin, label, description, point, "/ecofitness/images/"+picture.getName());
			
		} catch (IOException e) {
			e.printStackTrace();return false;
		}}
		else
			return articleDao.addArticle(admin, label, description, point, "");
	}

	public boolean updateArticle(int idArticle, String label,String description, int point,FileItem photo,String dir) {
		try {
			if(ImageIO.read(photo.getInputStream())!=null){
			String path=dir+"images\\";
			File folder = new File(path);
			if(!folder.exists())
				folder.mkdirs();
			
			String pathArticle=path+"\\"+FilenameUtils.getName(photo.getName());
			
			File picture = new File(pathArticle);
			
			if(picture.exists()){
				pathArticle=FilenameUtils.removeExtension(pathArticle);
				pathArticle+=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
				pathArticle+="."+photo.getName().split("\\.")[photo.getName().split("\\.").length-1];
			}
			
			
			try {
				picture=new File(pathArticle);
				picture.createNewFile();
				OutputStream out = new FileOutputStream(pathArticle);
				InputStream in=photo.getInputStream();
				ImageIO.write(ImageIO.read(in),"jpg",out );
				out.close();in.close();
				return articleDao.updateArticle(idArticle, label, description, point, "/ecofitness/images/"+picture.getName());
				
			} catch (IOException e) {
				e.printStackTrace();return false;
			}}
			else
				return articleDao.updateArticle(idArticle, label, description, point, "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return articleDao.updateArticle(idArticle, label, description, point, "");
		}

	}

	public List<Article> getAllArticle() {
		return articleDao.getAllArticle();
	}

	public Article getArticle(int id) {
		return articleDao.getArticle(id);
	}
	
}
